<?php

use \GatewayWorker\Lib\Gateway;  

require_once dirname(__DIR__) . '/public.class.model.php';
require_once dirname(__DIR__) . '/Mahjong.php';
class Play_Model extends Public_Model
{
	
	/*
		出牌
	*/
	public function chooseCard($arrData)
	{
		$data 			= $arrData['data'];
		$operation 		= $arrData['operation'];
		$account_id 	= $arrData['account_id'];
		$client_id 		= $arrData['client_id'];

		$session 		= $arrData['session'];
		$Verification_Model = Verification_Model::getModelObject();
		if(false == $Verification_Model->checkRequestSession($account_id,$session))
		{
			return OPT_CONST::NO_RETURN;
		}

		if(empty($data['room_id']) || $data['room_id']<=0)
		{
			$this->writeLog("function(chooseCard):lack of room_id"." in file".__FILE__." on Line ".__LINE__);
			return $this->_missingPrameterArr($operation,"room_id");
		}
		
		$room_id = $data['room_id'];
		$card = $data['card'];

		$is_legal = $this->checkRequestClientLegal($client_id,$room_id,$account_id);
		if(!$is_legal)
		{
			$this->logMessage('error', "function(checkRequestClientLegal):illegal opt: account_id($account_id) room_id($room_id)"." in file".__FILE__." on Line ".__LINE__);
			//推送强制下线
			$illegal_message = $this->_JSON(array("result"=>"-203","result_message"=>"illegal opt")); 
			Gateway::sendToCurrentClient($illegal_message);
			return OPT_CONST::NO_RETURN;
		}

		$active_user = $this->queryActiveUser($room_id);
		if($active_user != $account_id){
			return array("result"=>OPT_CONST::FAILED,"operation"=>$operation,"data"=>array('active_user'=>$active_user),"result_message"=>"未轮到你操作");
		}

		$new_card = $this->queryNewCard($room_id);
		if($new_card != $card){
			$hand_card_array = $this->queryPlayerCard($room_id, $account_id);
			if(!in_array($card, $hand_card_array)){
				return array("result"=>"-2","operation"=>$operation,"data"=>array('card'=>$card),"result_message"=>"请刷新获取正确手牌");
			}
		}
		
		$Redis_Model = Redis_Model::getModelObject();
		
		$this->deleteRoomTimer($room_id);
		
		//更新当前出牌
		//$this->removePlayerCardToDiscard($room_id, $account_id, $card);
		$success = $this->removePlayerCardToDiscard($room_id, $account_id, $card);
		if($success !== OPT_CONST::SUCCESS)
		{
			return OPT_CONST::NO_RETURN;
		}
			
		$msg_arr = array(
			'result' => 0,
			'operation' => 'ThrowOutCard',
			'result_message' => "玩家出牌",
			'data' => array(
				'account_id' => $account_id,
				'card'  => $card,
				'is_passive' => 0
				)
			);
		$this->writeLog("[$room_id] ($account_id) 打牌($card)");

		$this->pushMessageToGroup($room_id, $msg_arr);

		if($this->checkPeng($room_id, $account_id, $card)){
			//有人碰杠
		} else {
			$next_account_id = $this->takeTurns($room_id);
			$this->notyUserToChoose($room_id, $next_account_id, $has_new_card = true);
		}
		return OPT_CONST::NO_RETURN;
	}


	/*
		碰
	*/
	public function pengCard($arrData)
	{
		$timestamp = time();
		$result = array();
		
		$data 			= $arrData['data'];
		$operation 		= $arrData['operation'];
		$account_id 	= $arrData['account_id'];
		$client_id 		= $arrData['client_id'];

		$session 		= $arrData['session'];
		$Verification_Model = Verification_Model::getModelObject();
		if(false == $Verification_Model->checkRequestSession($account_id,$session))
		{
			return OPT_CONST::NO_RETURN;
		}

		if(empty($data['room_id']) || $data['room_id']<=0)
		{
			$this->writeLog("function(chooseCard):lack of room_id"." in file".__FILE__." on Line ".__LINE__);
			return $this->_missingPrameterArr($operation,"room_id");
		}
		
		$room_id = $data['room_id'];

		$is_legal = $this->checkRequestClientLegal($client_id,$room_id,$account_id);
		if(!$is_legal)
		{
			$this->logMessage('error', "function(checkRequestClientLegal):illegal opt: account_id($account_id) room_id($room_id)"." in file".__FILE__." on Line ".__LINE__);
			//推送强制下线
			$illegal_message = $this->_JSON(array("result"=>"-203","result_message"=>"illegal opt")); 
			Gateway::sendToCurrentClient($illegal_message);
			return OPT_CONST::NO_RETURN;
		}

		$active_user = $this->queryActiveUser($room_id);
		if($active_user != $account_id){
			return array("result"=>OPT_CONST::FAILED,"operation"=>$operation,"data"=>array('active_user'=>$active_user),"result_message"=>"未轮到你操作");
		}
		
		$this->deleteRoomTimer($room_id);

		$this->pengFunc($room_id, $account_id, 0);
	
		return OPT_CONST::NO_RETURN;
	}

	/*
		杠   type 1暗杠  2加杠  3包杠 
	*/
	public function baoGang($arrData)
	{
		$timestamp = time();
		$result = array();
		
		$data 			= $arrData['data'];
		$operation 		= $arrData['operation'];
		$account_id 	= $arrData['account_id'];
		$client_id 		= $arrData['client_id'];

		$session 		= $arrData['session'];
		$Verification_Model = Verification_Model::getModelObject();
		if(false == $Verification_Model->checkRequestSession($account_id,$session))
		{
			return OPT_CONST::NO_RETURN;
		}

		if(empty($data['room_id']) || $data['room_id']<=0)
		{
			$this->writeLog("function(baoGang):lack of room_id"." in file".__FILE__." on Line ".__LINE__);
			return $this->_missingPrameterArr($operation,"room_id");
		}
		
		$room_id = $data['room_id'];

		$is_legal = $this->checkRequestClientLegal($client_id,$room_id,$account_id);
		if(!$is_legal)
		{
			$this->logMessage('error', "function(checkRequestClientLegal):illegal opt: account_id($account_id) room_id($room_id)"." in file".__FILE__." on Line ".__LINE__);
			//推送强制下线
			$illegal_message = $this->_JSON(array("result"=>"-203","result_message"=>"illegal opt")); 
			Gateway::sendToCurrentClient($illegal_message);
			return OPT_CONST::NO_RETURN;
		}

		$active_user = $this->queryActiveUser($room_id);
		if($active_user != $account_id){
			return array("result"=>OPT_CONST::FAILED,"operation"=>$operation,"data"=>array('active_user'=>$active_user),"result_message"=>"未轮到你操作");
		}
		
		$this->deleteRoomTimer($room_id);

		$this->baoGangFunc($room_id, $account_id, 0);
	
		return OPT_CONST::NO_RETURN;
	}

	/*
		暗杠   type 1暗杠  2加杠  3包杠 
	*/
	public function anGang($arrData)
	{
		$timestamp = time();
		$result = array();
		
		$data 			= $arrData['data'];
		$operation 		= $arrData['operation'];
		$account_id 	= $arrData['account_id'];
		$client_id 		= $arrData['client_id'];

		$session 		= $arrData['session'];
		$Verification_Model = Verification_Model::getModelObject();
		if(false == $Verification_Model->checkRequestSession($account_id,$session))
		{
			return OPT_CONST::NO_RETURN;
		}

		if(empty($data['room_id']) || $data['room_id']<=0)
		{
			$this->writeLog("function(anGang):lack of room_id"." in file".__FILE__." on Line ".__LINE__);
			return $this->_missingPrameterArr($operation,"room_id");
		}

		$room_id = $data['room_id'];

		$is_legal = $this->checkRequestClientLegal($client_id,$room_id,$account_id);
		if(!$is_legal)
		{
			$this->logMessage('error', "function(checkRequestClientLegal):illegal opt: account_id($account_id) room_id($room_id)"." in file".__FILE__." on Line ".__LINE__);
			//推送强制下线
			$illegal_message = $this->_JSON(array("result"=>"-203","result_message"=>"illegal opt")); 
			Gateway::sendToCurrentClient($illegal_message);
			return OPT_CONST::NO_RETURN;
		}

		$active_user = $this->queryActiveUser($room_id);
		if($active_user != $account_id){
			return array("result"=>OPT_CONST::FAILED,"operation"=>$operation,"data"=>array('active_user'=>$active_user),"result_message"=>"未轮到你操作");
		}

		$this->deleteRoomTimer($room_id);

		$this->anGangFunc($room_id, $account_id, 0);
	
		return OPT_CONST::NO_RETURN;
	}


	/*
		加杠(补杠，小明杠)
	*/
	public function jiaGang($arrData)
	{
		$timestamp = time();
		$result = array();
		
		$data 			= $arrData['data'];
		$operation 		= $arrData['operation'];
		$account_id 	= $arrData['account_id'];
		$client_id 		= $arrData['client_id'];

		$session 		= $arrData['session'];
		$Verification_Model = Verification_Model::getModelObject();
		if(false == $Verification_Model->checkRequestSession($account_id,$session))
		{
			return OPT_CONST::NO_RETURN;
		}

		if(empty($data['room_id']) || $data['room_id']<=0)
		{
			$this->writeLog("function(jiaGang):lack of room_id"." in file".__FILE__." on Line ".__LINE__);
			return $this->_missingPrameterArr($operation,"room_id");
		}
		
		$room_id = $data['room_id'];

		$is_legal = $this->checkRequestClientLegal($client_id,$room_id,$account_id);
		if(!$is_legal)
		{
			$this->logMessage('error', "function(checkRequestClientLegal):illegal opt: account_id($account_id) room_id($room_id)"." in file".__FILE__." on Line ".__LINE__);
			//推送强制下线
			$illegal_message = $this->_JSON(array("result"=>"-203","result_message"=>"illegal opt")); 
			Gateway::sendToCurrentClient($illegal_message);
			return OPT_CONST::NO_RETURN;
		}

		$active_user = $this->queryActiveUser($room_id);
		if($active_user != $account_id){
			return array("result"=>OPT_CONST::FAILED,"operation"=>$operation,"data"=>array('active_user'=>$active_user),"result_message"=>"未轮到你操作");
		}
		
		$this->deleteRoomTimer($room_id);

		$this->jiaGangFunc($room_id, $account_id, 0);

		return OPT_CONST::NO_RETURN;
	}

	/*
		抢杠胡
	*/
	public function qiangGangHu($arrData)
	{
		$data 			= $arrData['data'];
		$operation 		= $arrData['operation'];
		$account_id 	= $arrData['account_id'];
		$client_id 		= $arrData['client_id'];

		$session 		= $arrData['session'];
		$Verification_Model = Verification_Model::getModelObject();
		if(false == $Verification_Model->checkRequestSession($account_id,$session))
		{
			return OPT_CONST::NO_RETURN;
		}

		if(empty($data['room_id']) || $data['room_id']<=0)
		{
			$this->writeLog("function(qiangGangHu):lack of room_id"." in file".__FILE__." on Line ".__LINE__);
			return $this->_missingPrameterArr($operation,"room_id");
		}

		if(!isset($data['qiang']) || !in_array($data['qiang'], [0,1]) )
		{
			$this->writeLog("function(qiangGangHu):lack of qiang"." in file".__FILE__." on Line ".__LINE__);
			return $this->_missingPrameterArr($operation,"qiang");
		}

		if(!isset($data['card']) )
		{
			$this->writeLog("function(qiangGangHu):lack of card"." in file".__FILE__." on Line ".__LINE__);
			return $this->_missingPrameterArr($operation,"card");
		}
		
		$room_id = $data['room_id'];
		$qiang = $data['qiang'];	//0 过   1抢杠胡
		$card = $data['card'];

		$is_legal = $this->checkRequestClientLegal($client_id,$room_id,$account_id);
		if(!$is_legal)
		{
			$this->logMessage('error', "function(checkRequestClientLegal):illegal opt: account_id($account_id) room_id($room_id)"." in file".__FILE__." on Line ".__LINE__);
			//推送强制下线
			$illegal_message = $this->_JSON(array("result"=>"-203","result_message"=>"illegal opt")); 
			Gateway::sendToCurrentClient($illegal_message);
			return OPT_CONST::NO_RETURN;
		}

		$Redis_Model = Redis_Model::getModelObject();
		$replyArr = array("[roomid]"=>$room_id);
		$DaiQiangGang_Key = strtr(Redis_Const::DaiQiangGang_Key, $replyArr);
		$YiQiangGang_Key = strtr(Redis_Const::YiQiangGang_Key, $replyArr);
		$can_qiang =  $Redis_Model->sismemberSet($DaiQiangGang_Key, $account_id);
		if(!$can_qiang){
			return array("result"=>OPT_CONST::FAILED,"operation"=>$operation,"data"=>[],"result_message"=>"没有抢杠条件");
		}
		
		$this->deleteRoomTimer($room_id);

		$Redis_Model->smoveSet($DaiQiangGang_Key, $YiQiangGang_Key, [$account_id]);

		if($qiang){
			$loser_id = $this->queryActiveUser($room_id);
			$base_score = $this->queryBaseScore($room_id);
			$msg_arr = array(
				'result' => 0,
				'operation' => 'QiangGangHu',
				'result_message' => "抢杠胡",
				'data' => array(
					'account_id' => $account_id,
					'card' => $card,
					'loser_id' => $loser_id,
					'score' => 3 * 2 * $base_score
					)
				);
			$this->pushMessageToGroup($room_id, $msg_arr);

			$this->setQiangGang($room_id);

			//抢杠胡实时计分
			$this->balanceQiangScore($room_id, $account_id, $loser_id);

		}

		if($Redis_Model->scard($DaiQiangGang_Key) == 0){

			if($this->queryQiangGang($room_id)){
				$this->showWin($room_id, -1);
				$this->writeLog("[$room_id] 抢杠胡结束");
			} else {

				$active_user = $this->queryActiveUser($room_id);
				$this->balanceJiaGangScore($room_id, $active_user);
				//通知前端计分
				$msg_arr = array(
					'result' => 0,
					'operation' => 'JiaGangScore',
					'result_message' => "加杠计分",
					'data' => array(
						'account_id' => $active_user,
						'score' => $this->queryBaseScore($room_id)
						)
					);
				$this->pushMessageToGroup($room_id, $msg_arr);

				$this->notyUserToChoose($room_id, $active_user, $has_new_card = true);

				$this->writeLog("[$room_id] ($account_id) 抢杠胡：过");
			}
		}
		return OPT_CONST::NO_RETURN;
	}



	/*
		过
	*/
	public function passCard($arrData)
	{
		$timestamp = time();
		$result = array();
		
		$data 			= $arrData['data'];
		$operation 		= $arrData['operation'];
		$account_id 	= $arrData['account_id'];
		$client_id 		= $arrData['client_id'];

		$session 		= $arrData['session'];
		$Verification_Model = Verification_Model::getModelObject();
		if(false == $Verification_Model->checkRequestSession($account_id,$session))
		{
			return OPT_CONST::NO_RETURN;
		}

		if(empty($data['room_id']) || $data['room_id']<=0)
		{
			$this->writeLog("function(chooseCard):lack of room_id"." in file".__FILE__." on Line ".__LINE__);
			return $this->_missingPrameterArr($operation,"room_id");
		}
		
		$room_id = $data['room_id'];

		$is_legal = $this->checkRequestClientLegal($client_id,$room_id,$account_id);
		if(!$is_legal)
		{
			$this->logMessage('error', "function(checkRequestClientLegal):illegal opt: account_id($account_id) room_id($room_id)"." in file".__FILE__." on Line ".__LINE__);
			//推送强制下线
			$illegal_message = $this->_JSON(array("result"=>"-203","result_message"=>"illegal opt")); 
			Gateway::sendToCurrentClient($illegal_message);
			return OPT_CONST::NO_RETURN;
		}

		$active_user = $this->queryActiveUser($room_id);
		if($active_user != $account_id){
			return array("result"=>OPT_CONST::FAILED,"operation"=>$operation,"data"=>array('active_user'=>$active_user),"result_message"=>"未轮到你操作");
		}
		
		$this->deleteRoomTimer($room_id);

		$next_account_id = $this->takeTurns($room_id);
		$this->notyUserToChoose($room_id, $next_account_id, $has_new_card = true);
	
		return OPT_CONST::NO_RETURN;
	}

	/*
		胡了
	*/
	public function huCard($arrData)
	{
		$data 			= $arrData['data'];
		$operation 		= $arrData['operation'];
		$account_id 	= $arrData['account_id'];
		$client_id 		= $arrData['client_id'];

		$session 		= $arrData['session'];
		$Verification_Model = Verification_Model::getModelObject();
		if(false == $Verification_Model->checkRequestSession($account_id,$session))
		{
			return OPT_CONST::NO_RETURN;
		}

		if(empty($data['room_id']) || $data['room_id']<=0)
		{
			$this->writeLog("function(chooseCard):lack of room_id"." in file".__FILE__." on Line ".__LINE__);
			return $this->_missingPrameterArr($operation,"room_id");
		}
		
		$room_id = $data['room_id'];

		$is_legal = $this->checkRequestClientLegal($client_id,$room_id,$account_id);
		if(!$is_legal)
		{
			$this->logMessage('error', "function(checkRequestClientLegal):illegal opt: account_id($account_id) room_id($room_id)"." in file".__FILE__." on Line ".__LINE__);
			//推送强制下线
			$illegal_message = $this->_JSON(array("result"=>"-203","result_message"=>"illegal opt")); 
			Gateway::sendToCurrentClient($illegal_message);
			return OPT_CONST::NO_RETURN;
		}

		$active_user = $this->queryActiveUser($room_id);
		if($active_user != $account_id){
			return array("result"=>OPT_CONST::FAILED,"operation"=>$operation,"data"=>array('active_user'=>$active_user),"result_message"=>"未轮到你操作");
		}
		
		$Redis_Model = Redis_Model::getModelObject();
		
		$this->deleteRoomTimer($room_id);

		$this->showWin($room_id, $account_id);
	
		return OPT_CONST::NO_RETURN;
	}

	

	/*
		发送声音
	*/
	public function broadcastVoice($arrData)
	{
		$data 			= $arrData['data'];
		$operation 		= $arrData['operation'];
		$account_id 	= $arrData['account_id'];
		$client_id 		= $arrData['client_id'];

		$session 		= $arrData['session'];
		$Verification_Model = Verification_Model::getModelObject();
		if(false == $Verification_Model->checkRequestSession($account_id,$session))
		{
			return OPT_CONST::NO_RETURN;
		}
		
		if(!isset($data['room_id']) || $data['room_id']<=0)
		{
			$this->logMessage('error', "function(broadcastVoice):lack of room_id"." in file".__FILE__." on Line ".__LINE__);
			return $this->_missingPrameterArr($operation,"room_id");
		}
		
		$room_id = $data['room_id'];
		$voice_num = $data['voice_num'];

		$is_legal = $this->checkRequestClientLegal($client_id,$room_id,$account_id);
		if(!$is_legal)
		{
			$this->logMessage('error', "function(checkRequestClientLegal):illegal opt: account_id($account_id) room_id($room_id)"." in file".__FILE__." on Line ".__LINE__);
			//推送强制下线
			$illegal_message = $this->_JSON(array("result"=>"-203","result_message"=>"illegal opt")); 
			Gateway::sendToCurrentClient($illegal_message);
			return OPT_CONST::NO_RETURN;
		}
		
		$msg_arr = array("result"=>"0","operation"=>$operation,"data"=>array(
			'account_id'=>$account_id,
			'voice_num' =>$voice_num
			),"result_message"=>"发送声音");
		$this->pushMessageToGroup($room_id, $msg_arr, $client_id);
		return OPT_CONST::NO_RETURN;
	}

	/*
		发送声音
	*/
	public function speakVoice($arrData)
	{
		$data 			= $arrData['data'];
		$operation 		= $arrData['operation'];
		$account_id 	= $arrData['account_id'];
		$client_id 		= $arrData['client_id'];

		$session 		= $arrData['session'];
		$Verification_Model = Verification_Model::getModelObject();
		if(false == $Verification_Model->checkRequestSession($account_id,$session))
		{
			return OPT_CONST::NO_RETURN;
		}
		
		if(!isset($data['room_id']) || $data['room_id']<=0)
		{
			$this->writeLog("function(speakVoice):lack of room_id"." in file".__FILE__." on Line ".__LINE__);
			return $this->_missingPrameterArr($operation,"room_id");
		}
		if(!isset($data['local_id']))
		{
			$this->writeLog("function(speakVoice):lack of local_id"." in file".__FILE__." on Line ".__LINE__);
			return $this->_missingPrameterArr($operation,"local_id");
		}
		if(!isset($data['time']))
		{
			$this->writeLog("function(speakVoice):lack of time"." in file".__FILE__." on Line ".__LINE__);
			return $this->_missingPrameterArr($operation,"time");
		}
		if(!isset($data['server_id']))
		{
			$this->writeLog("function(speakVoice):lack of server_id"." in file".__FILE__." on Line ".__LINE__);
			return $this->_missingPrameterArr($operation,"server_id");
		}
		
		$room_id = $data['room_id'];
		$local_id = $data['local_id'];
		$time = $data['time'];
		$server_id = $data['server_id'];

		$is_legal = $this->checkRequestClientLegal($client_id,$room_id,$account_id);
		if(!$is_legal)
		{
			$this->logMessage('error', "function(checkRequestClientLegal):illegal opt: account_id($account_id) room_id($room_id)"." in file".__FILE__." on Line ".__LINE__);
			//推送强制下线
			$illegal_message = $this->_JSON(array("result"=>"-203","result_message"=>"illegal opt")); 
			Gateway::sendToCurrentClient($illegal_message);
			return OPT_CONST::NO_RETURN;
		}
		
		$msg_arr = array("result"=>"0","operation"=>$operation,"data"=>array(
			'account_id'=>$account_id,
			'local_id' =>$local_id,
			'time' =>$time,
			'server_id' =>$server_id
			),"result_message"=>"说话声音");
		$this->pushMessageToGroup($room_id, $msg_arr, $client_id);
		return OPT_CONST::NO_RETURN;
	}

	/*
		观察员用于调试收取房间消息

	public function observer($arrData)
	{
		$timestamp = time();
		$result = array();
		
		$data 			= $arrData['data'];
		$operation 		= $arrData['operation'];
		$account_id 	= $arrData['account_id'];
		$client_id 		= $arrData['client_id'];

		$session 		= $arrData['session'];
		$Verification_Model = Verification_Model::getModelObject();
		if(false == $Verification_Model->checkRequestSession($account_id,$session))
		{
			return OPT_CONST::NO_RETURN;
		}
		
		if(!isset($data['room_id']) )
		{
			$this->logMessage('error', "function(chosePlayType):lack of room_id"." in file".__FILE__." on Line ".__LINE__);
			return $this->_missingPrameterArr($operation,"room_id");
		}
		
		$room_id = $data['room_id'];
		
		//绑定用户UID
		// Gateway::bindUid($client_id, $account_id);
		if($room_id > 0){
			Gateway::joinGroup($client_id, $room_id);
			$room_id_str = $room_id;
		} else {
			$MMYSQL = $this->initMysql();
			$query = $MMYSQL->query("Select room_id from ".Room." where is_delete=0 and is_close=0 order by room_id desc limit 10");
			if(count($query) > 0){
				$room_ids = array();
				foreach ($query as $row) {
					$room_ids[] = $row['room_id'];
					Gateway::joinGroup($client_id, $row['room_id']);
				}
				$room_id_str = implode(",", $room_ids);
			} else {
				$room_id_str = "error";
			}
		}
		return array("operation"=>$operation,"data"=>array(),"result_message"=>"观察员".$client_id."成功潜入房间".$room_id_str);
	}
        */

	/*
		获取在线数
	*/
	public function LxhServer($arrData)
	{
		$data                   = $arrData['data'];
		$operation              = $arrData['operation'];
		$client_count = Gateway::getAllClientCount();
		return array("result"=>0,"operation"=>$operation,"data"=>array('client_count'=>$client_count),"result_message"=>"当前在线连接总数");
	}

	
}
